package com.ruoyi.exam.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.exam.domain.entity.exentity.WrongQuestionDailyStatDTO;
import com.ruoyi.exam.domain.entity.exentity.WrongQuestionStatDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.exam.domain.entity.ExamPaperQuestion;
import com.ruoyi.exam.domain.entity.Question;
import com.ruoyi.exam.domain.entity.TWrongQuestion;
import com.ruoyi.exam.mapper.TWrongQuestionMapper;
import com.ruoyi.exam.service.ITWrongQuestionService;
import com.ruoyi.exam.mapper.ExamPaperQuestionMapper;
import com.ruoyi.exam.mapper.QuestionMapper;

/**
 * 错题本Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-27
 */
@Service
public class TWrongQuestionServiceImpl implements ITWrongQuestionService {

    @Autowired
    private TWrongQuestionMapper tWrongQuestionMapper;

    @Autowired
    private ExamPaperQuestionMapper examPaperQuestionMapper;

    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 查询错题本
     * 
     * @param id 错题本主键
     * @return 错题本
     */
    @Override
    public TWrongQuestion selectTWrongQuestionById(Long id) {
        TWrongQuestion wrongQuestion = tWrongQuestionMapper.selectTWrongQuestionById(id);
        if (wrongQuestion != null) {
            // 填充题目信息
            wrongQuestion
                    .setQuestion(examPaperQuestionMapper.selectExamPaperQuestionById(wrongQuestion.getQuestionId()));
        }
        return wrongQuestion;
    }

    /**
     * 查询错题本列表
     * 
     * @param tWrongQuestion 错题本
     * @return 错题本
     */
    @Override
    public List<TWrongQuestion> selectTWrongQuestionList(TWrongQuestion tWrongQuestion) {
        List<TWrongQuestion> list = tWrongQuestionMapper.selectTWrongQuestionList(tWrongQuestion);
        // 填充题目信息
        for (TWrongQuestion wrongQuestion : list) {
            if (wrongQuestion.getQuestionId() != null) {
                ExamPaperQuestion selectExamPaperQuestionById;
                // 如果是每日一练任务，这里需要去关联t_question；如果不是每日一练，需要关联t_exam_paper_question
                if (wrongQuestion.getIsdaily() == 1) {
                    Question selectQuestionById = questionMapper.selectQuestionById(wrongQuestion.getQuestionId());
                    if(selectQuestionById == null){
                        continue;
                    }
                    selectExamPaperQuestionById = convertQuestionToExamPaperQuestion(selectQuestionById);
                } else {
                    selectExamPaperQuestionById = examPaperQuestionMapper
                            .selectExamPaperQuestionById(wrongQuestion.getQuestionId());
                }
                wrongQuestion.setQuestion(selectExamPaperQuestionById);
            }
        }
        return list;
    }

    private ExamPaperQuestion convertQuestionToExamPaperQuestion(Question question) {
        ExamPaperQuestion paperQuestion = new ExamPaperQuestion();
        paperQuestion.setQuestionType(question.getQuestionType());
        paperQuestion.setQuestionTitle(question.getQuestionTitle());
        paperQuestion.setQuestionText(question.getQuestionText());
        paperQuestion.setQuestionAnswer(question.getQuestionAnswer());
        paperQuestion.setQuestionAnswerExplain(question.getQuestionAnswerExplain());
        paperQuestion.setQuestionScore(question.getQuestionScore());
        paperQuestion.setCreateTime(new Date());
        paperQuestion.setUpdateTime(new Date());
        paperQuestion.setId(question.getId());
        paperQuestion.setDifficult(question.getDifficult());
        paperQuestion.setOutline(question.getOutline());
        return paperQuestion;
    }

    /**
     * 新增错题本
     * 
     * @param tWrongQuestion 错题本
     * @return 结果
     */
    @Override
    public int insertTWrongQuestion(TWrongQuestion tWrongQuestion) {
        tWrongQuestion.setCreateTime(DateUtils.getNowDate());
        return tWrongQuestionMapper.insertTWrongQuestion(tWrongQuestion);
    }

    /**
     * 修改错题本
     * 
     * @param tWrongQuestion 错题本
     * @return 结果
     */
    @Override
    public int updateTWrongQuestion(TWrongQuestion tWrongQuestion) {
        tWrongQuestion.setUpdateTime(DateUtils.getNowDate());
        return tWrongQuestionMapper.updateTWrongQuestion(tWrongQuestion);
    }

    /**
     * 批量删除错题本
     * 
     * @param ids 需要删除的错题本主键
     * @return 结果
     */
    @Override
    public int deleteTWrongQuestionByIds(Long[] ids) {
        return tWrongQuestionMapper.deleteTWrongQuestionByIds(ids);
    }

    /**
     * 删除错题本信息
     * 
     * @param id 错题本主键
     * @return 结果
     */
    @Override
    public int deleteTWrongQuestionById(Long id) {
        return tWrongQuestionMapper.deleteTWrongQuestionById(id);
    }

    @Override
    public List<WrongQuestionStatDTO> selectWrongQuestionStatistics() {
        return tWrongQuestionMapper.selectWrongQuestionStatistics();
    }

    @Override
    public List<WrongQuestionDailyStatDTO> selectWrongQuestionDailyStatistics() {
        return tWrongQuestionMapper.selectWrongQuestionDailyStatistics();
    }


}
